﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using RazakiBLL;

namespace RazakiSite.UserControls.Forum
{
    public partial class UCMessageActions : BaseUserControl
    {
        
        #region Properties

        private int MessageId { get { return UtilitiesUI.GetQueryString<int>("id"); } }

        public bool ShowNew { get; set; }

        public bool ShowBackToList { get; set; }

        public bool ShowDelete { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigControls();
        }

        private void ConfigControls()
        {
            BtnNew.Visible = ShowNew;
            BtnBackToList.Visible = ShowBackToList;
            BtnDelete.Visible = ShowDelete;

            BtnDelete.OnClientClick = string.Format(UtilitiesUI.Sentences(ESentences.confirmJs), "حذف پرسش");
        }

        protected void Btn_Click(object sender, EventArgs e)
        {
            Button linkButton = (Button)sender;
            string url = string.Empty;
            string queryString = string.Empty;

            switch (linkButton.CommandName.ToLower())
            {
                case "backtolist":
                    url = "~/PagesForum/MessagesList.aspx";
                    break;

                case "new":
                    queryString = UtilitiesUI.SetQueryString(new Dictionary<string, string>
                                                                 {
                                                                     {"messagetype", "question"},
                                                                     {"messagepermission", "public"}
                                                                 });
                    url = string.Format("~/PagesForum/MessageEdit.aspx{0}", queryString);
                    break;

                case "delete":
                    UowRazaki uowRazaki = new UowRazaki();

                    uowRazaki.MessageForumRepository.RemoveById(MessageId);
                    uowRazaki.Save();

                    url = "~/PagesForum/MessagesList.aspx";
                    break;
            }

            if (!string.IsNullOrWhiteSpace(url))
                Response.Redirect(url);
        }

    }
}